<template>
  <div>
    <PageHeader :title="t('trade.实时商品销售排行') + ' - ' + t('trade.TOP10')" class="border-bottom p10">
      <template #right>
        <DatePick class="ml20" @change="changeDate" />
      </template>
    </PageHeader>
    <div class="p10">
      <el-table
        :data="tableData"
        style="width: 100%"
        class="cover-table"
        border
        @sort-change="handleSortChange"
        :default-sort="tableDefaultSort"
        :loading="loading"
        :key="tableKey"
      >
        <el-table-column type="index" :label="t('trade.序号')" width="80" />
        <el-table-column :prop="'skuName' + locale" :label="t('trade.商品名称')" />
        <el-table-column :prop="'saleAmount'" :label="t('trade.销售额')" sortable="custom" />
        <el-table-column :prop="'saleCount'" :label="t('trade.订单量')" sortable="custom" />
      </el-table>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch, computed } from "vue";
import { useI18n } from "vue-i18n";
import DatePick from "@/components/datePick.vue";
import Api from "@/config/api/api";
import moment from "moment";

const { t, locale } = useI18n();

const date = ref([]);
const sort = ref("saleAmountDesc");
const loading = ref(false);
const tableData = ref([]);
const tableKey = ref(0);
const tableDefaultSort = ref({
  prop: "saleAmount",
  order: "descending",
});
const changeDate = (v) => {
  date.value = v;
  getDataList();
};
const getDataList = () => {
  loading.value = true;
  const params = {
    startDate: date.value[0],
    endDate: date.value[1],
    sort: sort.value,
  };
  Api.skuSaleTop10(params)
    .then((res) => {
      tableData.value = res.data;
    })
    .finally(() => {
      loading.value = false;
    });
};

const handleSortChange = ({ column, prop, order }) => {
  // 排序 saleAmountAsc销售额升序 saleAmountDesc销售额降序 saleCountAsc订单量升序 saleCountDesc订单量降序
  let _sort = "";
  if (prop === "saleAmount") {
    _sort = order === "ascending" ? "saleAmountAsc" : "saleAmountDesc";
  } else if (prop === "saleCount") {
    _sort = order === "ascending" ? "saleCountAsc" : "saleCountDesc";
  }
  sort.value = _sort;
  getDataList();
};
</script>
<style lang="scss" scoped></style>
